package DAO;

import Model.Article;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: wsn_blog
 * @description
 * @author:王松年
 * @creat: 2021-07-12 10:41:09
 **/
public class ArticleDAO {
    //添加文章
    public static int insert(Article article) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into article(title,content,user_id) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, article.getTitle());
            statement.setString(2, article.getContent());
            statement.setInt(3, article.getUser_id());
            return statement.executeUpdate();
        } finally {
            DBUtil.close(connection, statement);
        }
    }

    //通过用户id查询文章
    public static List<Article> selectByUserId(Integer id) throws SQLException {
        List<Article> articleList = new ArrayList<Article>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id,title,content,user_id from article where user_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Article article = new Article();
                article.setId(resultSet.getInt("id"));
                article.setTitle(resultSet.getString("title"));
                article.setContent(resultSet.getString("content"));
                article.setUser_id(resultSet.getInt("user_id"));
                articleList.add(article);
            }
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return articleList;
    }

    //通过文章id查询文章
    public static Article selectById(Integer id) throws SQLException {
        Article article =null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id,title,content,user_id from article where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                article = new Article();
                article.setId(resultSet.getInt("id"));
                article.setTitle(resultSet.getString("title"));
                article.setContent(resultSet.getString("content"));
                article.setUser_id(resultSet.getInt("user_id"));
            }
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return article;
    }

    //修改文章
    public static  boolean change(Article article)throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update article set content=?,title=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, article.getContent());
            statement.setString(2, article.getTitle());
            statement.setInt(3, article.getId());
            return statement.executeUpdate() == 1;

        } finally {
            DBUtil.close(connection, statement);
        }
    }

    //删除文章
    public static boolean delete(int id) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from article where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            return statement.executeUpdate() == 1;
        } finally {
            DBUtil.close(connection, statement);
        }
    }

    //通过用户id删除文章
    public static boolean deleteByUserId(int id) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from article where user_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            return statement.executeUpdate() > 0;
        } finally {
            DBUtil.close(connection, statement);
        }
    }
}
